package com.evil.enterprise.controller;

import cn.hutool.core.bean.BeanUtil;
import com.evil.account.api.RemoteTemplateService;
import com.evil.account.api.RemoteUserService;
import com.evil.account.pojo.dto.excel.user.UserExportExcel;
import com.evil.common.account.dto.template.DownloadUserTemplateReqDTO;
import com.evil.common.account.dto.user.*;
import com.evil.common.account.util.LoginUtil;
import com.evil.common.core.entity.PageModel;
import com.evil.common.core.entity.R;
import com.evil.common.family.enums.SexEnum;
import com.evil.common.file.dto.FileRespDTO;
import com.evil.common.file.dto.UploadReqDTO;
import com.evil.common.file.util.PoiUtil;
import com.evil.common.file.constant.OssConstant;
import com.evil.common.file.enums.BusTypeEnum;
import com.evil.common.log.annotation.RequestLogAnnotation;
import com.evil.file.api.RemoteFileService;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户接口
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final LoginUtil loginUtil;

    private final RemoteFileService remoteFileService;

    private final RemoteUserService remoteUserService;

    private final RemoteTemplateService remoteTemplateService;

    /**
     * 获取用户分页列表
     *
     * @param pageModel pageModel
     * @return R
     */
    @PostMapping("/page")
    public R<PageInfo<UserPageRespDTO>> page(@Validated @RequestBody PageModel<UserPageReqDTO> pageModel) {
        pageModel.getParam().setEnterpriseId(null);
        return R.success(remoteUserService.page(pageModel));
    }

    /**
     * 获取用户基础信息分页列表
     *
     * @param pageModel pageModel
     * @return R
     */
    @PostMapping("/base-page")
    public R<PageInfo<UserBaseRespDTO>> basePage(@Validated @RequestBody PageModel<UserPageReqDTO> pageModel) {
        pageModel.getParam().setEnterpriseId(null);
        return R.success(remoteUserService.basePage(pageModel));
    }

    /**
     * 新增用户
     *
     * @param addUserReqDTO addUserReqDTO
     * @return R
     */
    @PostMapping("/add")
    @RequestLogAnnotation(isSaveRequestData = false, isSaveResponseData = false)
    public R<?> addUser(@Validated @RequestBody AddUserReqDTO addUserReqDTO) {
        addUserReqDTO.setEnterpriseId(null);
        remoteUserService.add(addUserReqDTO);
        return R.success();
    }

    /**
     * 编辑用户
     *
     * @param modifyUserReqDTO modifyUserReqDTO
     * @return R
     */
    @PostMapping("/modify")
    @RequestLogAnnotation(isSaveRequestData = false, isSaveResponseData = false)
    public R<?> modify(@Validated @RequestBody ModifyUserReqDTO modifyUserReqDTO) {
        modifyUserReqDTO.setEnterpriseId(null);
        remoteUserService.modify(modifyUserReqDTO);
        return R.success();
    }

    /**
     * 修改个人信息
     *
     * @param modifyUserReqDTO modifyUserReqDTO
     * @return R
     */
    @PostMapping("/personal/center/modify")
    public R<?> personalCenterModify(@Validated @RequestBody ModifyUserReqDTO modifyUserReqDTO) {
        modifyUserReqDTO.setEnterpriseId(null);
        modifyUserReqDTO.setUserId(loginUtil.getLoginUserId());
        remoteUserService.modify(modifyUserReqDTO);
        return R.success();
    }

    /**
     * 下载导入用户模板
     *
     * @param downloadUserTemplateReqDTO downloadUserTemplateReqDTO
     * @return R
     */
    @PostMapping("/download/template")
    public R<Object> downloadUserTemplate(@Validated @RequestBody DownloadUserTemplateReqDTO downloadUserTemplateReqDTO) {
        return R.success((Object) remoteTemplateService.downloadUserTemplate(downloadUserTemplateReqDTO));
    }

    /**
     * 导出用户
     *
     * @param pageModel pageModel
     * @return R
     */
    @PostMapping("/export")
    public R<?> exportExcel(@Validated @RequestBody PageModel<UserPageReqDTO> pageModel) {
        pageModel.getParam().setEnterpriseId(null);
        PageInfo<UserPageRespDTO> list = remoteUserService.page(pageModel);
        List<UserExportExcel> userExportExcelList = new ArrayList<>();
        list.getList().forEach(u -> {
            UserExportExcel userExportExcel = BeanUtil.copyProperties(u, UserExportExcel.class);
            if (u.getSex().equals(SexEnum.MALE.getId())) {
                userExportExcel.setSexName(SexEnum.MALE.getName());
            } else if (u.getSex().equals(SexEnum.FEMALE.getId())) {
                userExportExcel.setSexName(SexEnum.FEMALE.getName());
            }
            userExportExcelList.add(userExportExcel);
        });
        if (!userExportExcelList.isEmpty()) {
            PoiUtil.exportExcel(userExportExcelList, "用户信息情况", "用户表", UserExportExcel.class, "用户表.xlsx");
        } else {
            PoiUtil.exportExcel(new ArrayList<>(), "用户信息情况", "用户表", UserExportExcel.class, "用户表.xlsx");
        }
        return R.success();
    }

    /**
     * 获取用户数量
     *
     * @param userCountReqDTO queryUserAmountReqDTO
     * @return R
     */
    @PostMapping("/count")
    public R<UserCountRespDTO> userCount(@Validated @RequestBody UserCountReqDTO userCountReqDTO) {
        userCountReqDTO.setEnterpriseId(null);
        return R.success(remoteUserService.userCount(userCountReqDTO));
    }

    /**
     * 修改用户密码
     *
     * @param modifyPasswordReqDTO modifyPasswordReqDTO
     * @return R
     */
    @PostMapping("/password/modify")
    public R<?> updateUserPassword(@Validated @RequestBody ModifyPasswordReqDTO modifyPasswordReqDTO) {
        modifyPasswordReqDTO.setEnterpriseId(null);
        remoteUserService.modifyPassword(modifyPasswordReqDTO);
        return R.success();
    }

    /**
     * 修改用户密码
     *
     * @param modifyPasswordReqDTO modifyPasswordReqDTO
     * @return R
     */
    @PostMapping("/myself-password/modify")
    public R<?> updateMyselfPassword(@Validated @RequestBody ModifyPasswordReqDTO modifyPasswordReqDTO) {
        modifyPasswordReqDTO.setEnterpriseId(null);
        modifyPasswordReqDTO.setUserId(null);
        remoteUserService.modifyPassword(modifyPasswordReqDTO);
        return R.success();
    }

    /**
     * 重置用户密码
     *
     * @param resetPasswordReqDTO resetPasswordReqDTO
     * @return R
     */
    @PostMapping("/password/reset")
    public R<?> modifyUserPassword(@Validated @RequestBody ResetPasswordReqDTO resetPasswordReqDTO) {
        remoteUserService.resetPassword(resetPasswordReqDTO);
        return R.success();
    }

    /**
     * 用户详情
     *
     * @param userDetailReqDTO userDetailReqDTO
     * @return R
     */
    @PostMapping("/detail")
    public R<UserDetailRespDTO> userDetail(@Validated @RequestBody UserDetailReqDTO userDetailReqDTO) {
        userDetailReqDTO.setEnterpriseId(null);
        userDetailReqDTO.setUserId(loginUtil.getParamUserId(userDetailReqDTO.getUserId()));
        return R.success(remoteUserService.userDetail(userDetailReqDTO));
    }

    /**
     * 上传头像
     *
     * @param uploadReqDTO uploadReqDTO
     * @return R
     */
    @PostMapping("/portrait/upload")
    public R<FileRespDTO> uploadUserPortrait(@Validated UploadReqDTO uploadReqDTO) {
        uploadReqDTO.setType(BusTypeEnum.PORTRAIT.getId());
        uploadReqDTO.setEnterpriseId(null);
        uploadReqDTO.setUserId(null);
        uploadReqDTO.setAllowMaxSize(OssConstant.UPLOAD_USER_PORTRAIT_SIZE);
        uploadReqDTO.setAllowContentType(OssConstant.UPLOAD_USER_PORTRAIT_TYPE);
        return R.success(remoteFileService.uploadFile(uploadReqDTO));
    }

    /**
     * 修改用户头像
     *
     * @param modifyPortraitReqDTO modifyPortraitReqDTO
     * @return R
     */
    @PostMapping("/portrait/modify")
    public R<?> modifyUserPortrait(@Validated @RequestBody ModifyPortraitReqDTO modifyPortraitReqDTO) {
        modifyPortraitReqDTO.setEnterpriseId(null);
        remoteUserService.modifyUserPortrait(modifyPortraitReqDTO);
        return R.success();
    }

    /**
     * 修改用户头像
     *
     * @param modifyPortraitReqDTO modifyPortraitReqDTO
     * @return R
     */
    @PostMapping("/myself-portrait/modify")
    public R<?> modifyMyselfPortrait(@Validated @RequestBody ModifyPortraitReqDTO modifyPortraitReqDTO) {
        modifyPortraitReqDTO.setEnterpriseId(null);
        modifyPortraitReqDTO.setUserId(null);
        remoteUserService.modifyUserPortrait(modifyPortraitReqDTO);
        return R.success();
    }
}
